﻿using System;
using System.Windows.Interactivity;
using System.Windows.Controls;
using System.Windows;
using NthDownload.Common.Utils;

namespace NthDownload.Common.Interactivity.Behaviors
{
    public class ListBoxScrollOffsetBinder : Behavior<ListBox>
    {
        #region Override Methods

        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.Loaded -= AssociatedObjectLoaded;
            AssociatedObject.Loaded += AssociatedObjectLoaded;

            AssociatedObject.Unloaded -= AssociatedObject_Unloaded;
            AssociatedObject.Unloaded += AssociatedObject_Unloaded;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            AssociatedObject.Loaded -= AssociatedObjectLoaded;
            AssociatedObject.Unloaded -= AssociatedObject_Unloaded;
        }

        #endregion Override Methods

        #region Event Handlers

        private void AssociatedObjectLoaded(object sender, RoutedEventArgs e)
        {
            //
            // Get the listboxes scrollviewer
            //
            ScrollViewer sv = VisualTreeUtils.GetChildElementOfType(typeof(ScrollViewer), AssociatedObject) as ScrollViewer;
            
            if (sv == null)
            {
                return;
            }

            //
            // Scroll to offsets
            //
            sv.ScrollToVerticalOffset(VerticalOffset);
            sv.ScrollToHorizontalOffset(HorizontalOffset);
        }

        private void AssociatedObject_Unloaded(object sender, RoutedEventArgs e)
        {
            //
            // Get the listboxes scrollviewer
            //
            ScrollViewer sv = VisualTreeUtils.GetChildElementOfType(typeof(ScrollViewer), AssociatedObject) as ScrollViewer;

            if (sv == null)
            {
                return;
            }

            //
            // Store scroll offsets
            //
            VerticalOffset = sv.VerticalOffset;
            HorizontalOffset = sv.HorizontalOffset;
        }

        #endregion Event Handlers

        #region Dependancy Properties

        #region VerticalOffset

        public double VerticalOffset
        {
            get { return (double)GetValue(VerticalOffsetProperty); }
            set { SetValue(VerticalOffsetProperty, value); }
        }

        // Using a DependencyProperty as the backing store for VertialOffset.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VerticalOffsetProperty =
            DependencyProperty.Register("VerticalOffset", typeof(double), typeof(ListBoxScrollOffsetBinder), new UIPropertyMetadata(Convert.ToDouble(0)));

        #endregion VerticalOffset

        #region HorizontalOffset

        public double HorizontalOffset
        {
            get { return (double)GetValue(HorizontalOffsetProperty); }
            set { SetValue(HorizontalOffsetProperty, value); }
        }

        // Using a DependencyProperty as the backing store for VertialOffset.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HorizontalOffsetProperty =
            DependencyProperty.Register("HorizontalOffset", typeof(double), typeof(ListBoxScrollOffsetBinder), new UIPropertyMetadata(Convert.ToDouble(0)));

        #endregion HorizontalOffset

        #endregion Dependancy Properties
    }
}
